(function(angular) {
    "use strict";

    var ctl_module = angular.module("cloapCertificateApp.controller");

    ctl_module.controller("certificate_require.listCtl", function($scope, $modal,
            CertificateRequire, Position, CheckOrgan, CertificateType) {
        var modal_instance = null,
            certificate_require_form = {
                add: "/cloap/app/cloapCertificateApp/partials/certificate_require_add.html",
                edit: "/cloap/app/cloapCertificateApp/partials/certificate_require_edit.html"
            };
        $scope.widget_title = "持证要求";
        $scope.certificate_requires = CertificateRequire.query();

        $scope.editOrAdd = function(operate, certificate_require_id) {
            getDataForModify();

            var ctl = isAdd(operate) ? "certificate_require.addCtl" :
                                       "certificate_require.editCtl";

            modal_instance = $modal.open({
                scope: $scope,
                templateUrl: certificate_require_form[operate],
                controller: ctl,
                resolve: {
                    params: function() {
                        return {
                            certificate_require_id: certificate_require_id
                        };
                    }
                }
            });

            function isAdd(operate) {
                return operate === "add";
            }
        };

        $scope.saveCertificateRequire = function(save_method, data, callback) {
            CertificateRequire[save_method](data, callback, function(response) {
                alert(response.message || "保存失败");
            });
        };

        $scope.deleteCertificateRequire = function(certificate_require_id) {
            CertificateRequire.delete({certificate_require_id: certificate_require_id}, function() {
                $scope.$emit("change_certificate_require");
            });
        };

        $scope.$on("change_certificate_require", function() {
            $scope.certificate_requires = CertificateRequire.query();
        });

        $scope.closeModal = function() {
            modal_instance.close();
        };

        function getDataForModify() {
            $scope.positions = Position.all();
            $scope.check_organs = CheckOrgan.query();
            CertificateType.query(function(response_certificate_types) {
                $scope.certificate_types = response_certificate_types;
                $scope.$broadcast("certificate_type_loaded");
            });
        }
    });

    ctl_module.controller("certificate_require.editCtl", function($scope,
            CertificateRequire, params) {
        var save_method = "update";

        CertificateRequire.get({certificate_require_id: params.certificate_require_id},
                function(response_certificate_require) {
            $scope.certificate_require = {
                id: response_certificate_require.id,
                manager_id: response_certificate_require.manager.id,
                paper_type_id: response_certificate_require.paper_type.id,
                position_id: response_certificate_require.position.id
            };
        });
        $scope.widget_title = "编辑持证要求";

        $scope.save = function() {
            $scope.saveCertificateRequire(save_method, $scope.certificate_require,
                                          saveSuccessCallBack);
        };

        function saveSuccessCallBack() {
            $scope.$emit("change_certificate_require");
            $scope.closeModal();
        }
    });

    ctl_module.controller("certificate_require.addCtl", function($scope) {
        var save_method = "save";
        $scope.certificate_require = {};
        $scope.widget_title = "添加持证要求";
        $scope.page_info = {
            selected_certificate_types: [],
            current_selected: null
        };


        $scope.save = function() {
            processDataForAdd();
            $scope.saveCertificateRequire(save_method, $scope.certificate_require,
                                          saveSuccessCallBack);
        };

        $scope.$on("certificate_type_loaded", function() {
            $scope.page_info.usable_certificate_types = angular.copy($scope.certificate_types);
        });

        $scope.addCertificateType = function() {
            $scope.page_info.usable_certificate_types.splice(
                _.indexOf($scope.page_info.usable_certificate_types,
                          $scope.page_info.current_selected),
                1
            );
            $scope.page_info.selected_certificate_types.push($scope.page_info.current_selected);
            $scope.page_info.current_selected = null;
        };

        $scope.removeSelectedCertificateType = function(remove_certificate_type) {
            $scope.page_info.selected_certificate_types.splice(
                _.indexOf($scope.page_info.selected_certificate_types,
                    remove_certificate_type),
                1
            );
            $scope.page_info.usable_certificate_types.push(remove_certificate_type);
            $scope.page_info.current_selected = null;
        };

        function processDataForAdd() {
            $scope.certificate_require.paper_type_ids =
                _.pluck($scope.page_info.selected_certificate_types, "id");
        }

        function saveSuccessCallBack() {
            $scope.$emit("change_certificate_require");
            $scope.closeModal();
        }
    });

}(angular));
